#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->lineEdit->setText(QApplication::applicationDirPath());
}

MainWindow::~MainWindow()
{
    delete ui;
}

QString MainWindow::autoRename(QString fileName)
{
    QString newFileName = fileName;
    QFileInfo fileInfo(fileName);
    QString pathFileName = fileInfo.absolutePath() + "/" + fileInfo.baseName();   //不带扩展名的文件名
    QString suffix = fileInfo.completeSuffix();
    if(!suffix.isEmpty())
        suffix.insert(0,'.');

    int renCnt = 0;
    while(QFile::exists(newFileName)){
        newFileName = QString("%1(%2)%3").arg(pathFileName).arg(++renCnt).arg(suffix);
        qDebug()<<"文件："<<newFileName;
    }

    return newFileName;
}


void MainWindow::on_pushButton_chooseDir_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                    "",
                                                    QFileDialog::ShowDirsOnly
                                                    | QFileDialog::DontResolveSymlinks);

    ui->lineEdit->setText(dir);

}


void MainWindow::on_pushButton_new_file_clicked()
{
    QString fileName = ui->lineEdit->text() + "/" + ui->lineEdit_filename->text();

    QFile file(autoRename(fileName));
    file.open(QIODevice::ReadWrite);
    file.close();
}

